package com.jzg.jcptadmin.presenter;

import android.util.Log;

import com.jzg.jcptadmin.Utils.UIUtils;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.Canpei;
import com.jzg.jcptadmin.data.vo.RepairBean;
import com.jzg.jcptadmin.data.vo.SuccessValuationData;
import com.jzg.jcptadmin.viewinterface.OperationEvaluationInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/2/23.
 */
public class OperationEvaluationPresenter extends BasePresenter<OperationEvaluationInterface> {
    private DataManager dataManager;
    private OperationEvaluationInterface mView;
    private Subscription mSubscription;

    public OperationEvaluationPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void attachView(OperationEvaluationInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void getCanPei(String styleID) {
        Map<String, String> params = mView.getCanpeiParams(styleID);
        Log.e("jcpt_pgs", UIUtils.getUrl(params));
        if (!checkSubmitInfo(params)) return;
        mSubscription = dataManager.CanPeiData(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).subscribe(new Observer<Canpei>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e != null && mView != null) {
                            e.printStackTrace();
                            mView.showError(Constant.errr_net);
                        }
                    }

                    @Override
                    public void onNext(Canpei successData) {
                        mView.showCanPei(successData);
                    }
                });
    }

    public void OperationEvaluationData() {
        Map<String, String> params = mView.getEvaluationParams();
        if (!checkSubmitInfo(params)) return;
        mSubscription = dataManager.OperationEvaluationData1(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).subscribe(new Observer<SuccessValuationData>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (e != null) {
                            mView.showError(Constant.errr_net);
                        }
                    }

                    @Override
                    public void onNext(SuccessValuationData successData) {
                        mView.showresult(successData);
                    }
                });
    }

    public void getRepairLog(Map<String, String> params) {
        if (!checkSubmitInfo(params)) return;
        mSubscription = dataManager.getRepairLog(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).subscribe(new Observer<RepairBean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (e != null) {
                            mView.showError("查询失败");
                        }
                    }

                    @Override
                    public void onNext(RepairBean successData) {
                        if (successData.getStatus() == 100) {
                            mView.getRepairSuccess(successData);
                        } else {
                            mView.showError(successData.getMsg());
                        }
                    }
                });
    }

}
